package remin;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;

public class UpLis implements ItemListener,ActionListener{
	
	int cdatst,cpredst,cpriost,cpoznst;
	String updat;
			
	public void itemStateChanged(ItemEvent e) {
							
		Object zdroj = e.getItemSelectable();
		if(zdroj == UpFor.cdat){
			int status = e.getStateChange();
			if (status == ItemEvent.SELECTED){

				UpFor.den.setEnabled(true);
				UpFor.bodkad.setEnabled(true);
				UpFor.mesiac.setEnabled(true);
				UpFor.bodkam.setEnabled(true);
				UpFor.rr.setEnabled(true);
				UpFor.rok.setEnabled(true);
				UpFor.hod.setEnabled(true);
				UpFor.dvojb.setEnabled(true);
				UpFor.min.setEnabled(true);
				UpFor.okupr.setEnabled(true);
				cdatst = 1;
			}
			else{
				UpFor.den.setEnabled(false);
				UpFor.bodkad.setEnabled(false);
				UpFor.mesiac.setEnabled(false);
				UpFor.bodkam.setEnabled(false);
				UpFor.rr.setEnabled(false);
				UpFor.rok.setEnabled(false);
				UpFor.hod.setEnabled(false);
				UpFor.dvojb.setEnabled(false);
				UpFor.min.setEnabled(false);
				cdatst = 0;
			}
		}
		else if(zdroj == UpFor.cpred){
			int status = e.getStateChange();
			if (status == ItemEvent.SELECTED){
				UpFor.pret.setEnabled(true);
				UpFor.okupr.setEnabled(true);
				cpredst = 1;
			}
			else{
				UpFor.pret.setEnabled(false);
				cpredst = 0;
			}
			
		}
		else if(zdroj == UpFor.cprio){
			int status = e.getStateChange();
			if (status == ItemEvent.SELECTED){
				UpFor.priot.setEnabled(true);
				UpFor.okupr.setEnabled(true);
				cpriost = 1;
			}
			else{
				UpFor.priot.setEnabled(false);
				cpriost = 0;
			}
		}
		else if(zdroj == UpFor.cpozn){
			int status = e.getStateChange();
			if (status == ItemEvent.SELECTED){
				UpFor.pozt.setEnabled(true);
				UpFor.pozt.setBorder(BorderFactory.createLineBorder(Color.darkGray, 1));
				UpFor.okupr.setEnabled(true);
				cpoznst = 1;
			}
			else{
				UpFor.pozt.setEnabled(false);
				UpFor.pozt.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
				cpoznst = 0;
			}
		}
		
	}
	
	public void actionPerformed(ActionEvent evt){
		
		String com = evt.getActionCommand();
		DelUp dupp = new DelUp();
		String chdat,chpred,chprio,chpozn;
		chdat = chpred = chprio = chpozn = "";
		String dd,mm,yy,hh,mi,predm,prior,pozna;
		int stat = 0;
		
		if(com =="OK,uprav"){
			
			if(cdatst == 1){
			
			dd = UpFor.den.getText();
			mm = UpFor.mesiac.getText();
			yy = UpFor.rok.getText();
			hh = UpFor.hod.getText();
			mi = UpFor.min.getText();
			chdat =" datum_cas = TO_DATE('" + dd + "-" + mm + "-" + yy + " " + hh + ":" + mi
			+ "','DD-MM-YY HH24:MI')";
			stat = 1;
			System.out.println(chdat);
			
			}
			if(cpredst == 1){
				predm = UpFor.pret.getText().trim();
				 chpred = " predmet = '" +predm +"'";
				 if(stat ==1){
					 chpred ="," + chpred;
				 }
				 stat = 1;
				 System.out.println(chpred);
			}
			if(cpriost == 1){
				prior = (String)UpFor.priot.getSelectedItem();
				chprio = " priorita='" + prior + "'";
				if(stat ==1){
					 chprio ="," + chprio;
				 }
				 stat = 1;
				System.out.println(chprio);
			}
			if(cpoznst == 1){
				pozna = UpFor.pozt.getText().trim();
				chpozn = " poznamka ='" + pozna + "'";
				if(stat ==1){
					 chpozn ="," + chpozn;
				 }
				 stat = 1;
				System.out.println(chpozn);
			}
			updat ="update reminder set" + chdat + chpred + chprio + chpozn + 
			" where idrem = ";
			dupp.idRow();
			dupp.upRow(updat);
			
		}
	}
}
